package ui;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
 
import javacc.ParseException;
import javacc.SimpleNode;
import javacc.Token;
import javacc.XYZ2;
import javacc.XYZ2Constants;
import javacc.ParseException;
import javacc.JJTXYZ2State;
import javacc.Node;
import javacc.SimpleCharStream;
import javacc.XYZ2TreeConstants;
import javacc.XYZ2TokenManager;
import javacc.Token;
import javacc.TokenMgrError;
import javacc.XYZ2Constants;

import javax.print.attribute.standard.JobMessageFromOperator;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.table.AbstractTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import TAC.TACGenerator;

import util.SemanticException;
import util.TypeChecker;
import util._Exception;

import jsyntaxpane.DefaultSyntaxKit; 



public class MainWindow extends JFrame {
	private void FillNode(DefaultMutableTreeNode root, SimpleNode n) {
		int nCld = n.jjtGetNumChildren();
		//skip verbose recursions
		if (nCld==1) {
			SimpleNode cld = (SimpleNode) n.jjtGetChild(0);
			if (n.jjtGetFirstToken()==cld.jjtGetFirstToken() &&
					n.jjtGetLastToken()==cld.jjtGetLastToken() ) {
				//if have only one child and child contain same tokens
				//with |n|
				FillNode(root,cld);
				return;
			}
		}//if (nCld==1)
		Token curTk = n.jjtGetFirstToken();
		Token lastTk = null;
		if (nCld!=0) 
			lastTk = ((SimpleNode)n.jjtGetChild(0)).jjtGetFirstToken();
		else lastTk = n.jjtGetLastToken().next;
		while (curTk!=lastTk) {
			root.add(new DefaultMutableTreeNode(curTk.image));
			curTk = curTk.next;
		}
		for (int i = 0;i<nCld;i++) {
			SimpleNode curNode = (SimpleNode) n.jjtGetChild(i);
			DefaultMutableTreeNode nxt = new DefaultMutableTreeNode(curNode.toString());
			root.add(nxt);
			FillNode(nxt,(SimpleNode)n.jjtGetChild(i));
			curTk = curNode.jjtGetLastToken().next;
			lastTk = (i==nCld-1)?
					n.jjtGetLastToken().next:
					((SimpleNode)n.jjtGetChild(i+1)).jjtGetFirstToken();
			while (curTk!=lastTk) {
				root.add(new DefaultMutableTreeNode(curTk.image));
				curTk = curTk.next;
			}
		}
	}

	
	protected void semanticAnalysis() throws _Exception
	{
		InputStream ins = new ByteArrayInputStream( 
				getCodeEditor().getText().getBytes( ) );
		getProblemTextArea().setText("");//empty errors
		XYZ2 parser = new XYZ2(ins);
		Token beginToken = parser.token;
        setTokenTable(parser);
        parser.token = beginToken;
		SimpleNode n = null;
		try {
			 n = parser.Program();
			 if (!parser.exceptions.equals("")) {
				 throw new ParseException(parser.exceptions);
			 }
			 //parse OK
			 //start typecheck
			 try {
				 TypeChecker tc = new TypeChecker(n);
				 //check type 
				 tc.run();
				 if (tc.errors.size()!=0)
					 for(int i = 0;i<tc.errors.size();i++) {					 
						 System.out.println(tc.errors.get(i).getMsg());
						 setProblemTextArea(tc.errors.get(i).getMsg());
					 } 
				 else getProblemTextArea().setText("All Correct");
				 
				 
				//||||||||||||||||||||||added by ShuangCai 
				TACGenerator tgaGenerator=new TACGenerator(n,tc.classList,tc.agreeRules);
				tgaGenerator.Generate();
				 
			 }catch (SemanticException e) {
				 setProblemTextArea(e.getMessage());
				 return;
			 } catch(_Exception e) {
				 e.printStackTrace();
				 return;
			 }
		} catch (ParseException e) {
			setProblemTextArea(e.getMessage());
			e.printStackTrace();
			return;
		}
		
	}
	
	protected void parse() {
		InputStream ins = new ByteArrayInputStream( 
				getCodeEditor().getText().getBytes( ) );
		XYZ2 parser = new XYZ2(ins);
		Token beginToken = parser.token;
        setTokenTable(parser);
        parser.token = beginToken;
		SimpleNode n = null;
		try {
			 n = parser.Program();
			 //setTokenTextArea(n);
			 setProblemTextArea(parser);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			setProblemTextArea(e.getMessage());
			e.printStackTrace();
			return;
		}
		DefaultMutableTreeNode root = new DefaultMutableTreeNode();
		treeWeight=GetTreeWeight(n);
		System.out.println("Tree Weight: "+treeWeight);
		setConsoleTable(parser);
		
		FillNode(root,n);
		
		getJTree().setModel(new DefaultTreeModel(root));
		//jTree.setName("jTree");
		//Token cur = n.jjtGetFirstToken();
		/*while (cur!=n.jjtGetLastToken().next) {
			System.out.println(XYZ2Constants.tokenImage[cur.kind]+":"+
					cur.image);
			cur = cur.next;
		}*/
		parser.CountKeyWordAndId();
		//System.out.printf("id count=%d\n",parser.numOfId);
		//System.out.printf("keyword cout=%d\n",parser.numOfKeyWord);
		System.out.printf("assign stmt=%d,ret stmt=%d sysout stmt=%d\n",
				parser.nAssignStmt,parser.nRetStmt,parser.nSysOutStmt);
	}

	private int GetTreeWeight(SimpleNode n) {
		  int weight=0;
		  System.out.println(n.toString());
		  if (n.toString().equals("AssignStatement")||
			  n.toString().equals("SystemOutStatement")||
			  n.toString().equals("ReturnStatement"))

		 {
			  weight+=1;
		 }
		  else if (n.toString().equals("IfStatement")){
			  for (int i=0;i<n.jjtGetNumChildren();i++) {
				  weight+=GetTreeWeight((SimpleNode)n.jjtGetChild(i))*2;
			  }
		  }
		  else if (n.toString().equals("WhileStatement")){
			  for (int i=0;i<n.jjtGetNumChildren();i++) {
			  weight+=GetTreeWeight((SimpleNode)n.jjtGetChild(i))*4;
			  }
		  }

		  else {
			  for (int i=0;i<n.jjtGetNumChildren();i++) {
				  weight+=GetTreeWeight((SimpleNode)n.jjtGetChild(i));
			  }			  
			  
		  }
		 return weight;
	}
	
	/**
	 * This method initializes tabCodeEditor	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getTabCodeEditor() {
		if (tabCodeEditor == null) {
			tabCodeEditor = new JTabbedPane();
			tabCodeEditor.addTab("Untitled *", null, getScrCode(), null);
		}
		return tabCodeEditor;
	}

	/**
	 * This method initializes tabOutputPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getTabOutputPane() {
		if (tabOutputPane == null) {
			tabOutputPane = new JTabbedPane();
			tabOutputPane.addTab("token", null, getTokenPane(), null);
			tabOutputPane.addTab("console", null, getConsolePane(), null);
			tabOutputPane.addTab("problem", null, getProblemPane(), null);
			tabOutputPane.addTab("three-address code", null, getThreeAddressCodePane(), null);
		}
		return tabOutputPane;
	}

	/**
	 * This method initializes tokenTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getTokenTextArea() {
		if (tokenTextArea == null) {
			tokenTextArea = new JTextArea();
			tokenTextArea.setEditable(false);
		}
		return tokenTextArea;
	}
	
	/**
	 * This method modifies tokenTextArea
	 */
    private void setTokenTextArea(XYZ2 parser) {
		tokenTextArea.setText("");
		Token token = null;
		while ((token = parser.getNextToken()).image != "") {
		    System.out.println(token.image);
			tokenTextArea.append(XYZ2Constants.tokenImage[token.kind] + " : " + token.image +"\n");
		    if(token.kind == XYZ2.WrongID) {
				problemTextArea.setText("");
		    	setProblemTextArea("Lexical Error: WrongID achieved.\n" +
		    					   "Perhaps there is number at the begining of ID.\n");
		    	lError = true;
		    }
		}
		tokenTextArea.append("\n");
	}
    
	private void setTokenTextArea(SimpleNode n) {
		tokenTextArea.setText("");
		Token cur = n.jjtGetFirstToken();
		while (cur!=n.jjtGetLastToken().next) {
			tokenTextArea.append(XYZ2Constants.tokenImage[cur.kind]+":"+
                            cur.image+"\n");
            cur = cur.next;
		}
		tokenTextArea.append("\n");
	}
	
	/**
	 * This method initializes consoleTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getConsoleTextArea() {
		if (consoleTextArea == null) {
			consoleTextArea = new JTextArea();
			consoleTextArea.setEditable(false);
		}
		return consoleTextArea;
	}

	/**
	 * This method modifies consoleTextArea
	 */
	private void setConsoleTextArea(XYZ2 parser) {
		consoleTextArea.setText("");
        parser.CountKeyWordAndId();
		//consoleTextArea.append("id count = " + parser.numOfId +"\n");
		//consoleTextArea.append("keyword cout = " + parser.numOfKeyWord + "\n");
		consoleTextArea.append("assign stmt = " + parser.nAssignStmt + "\n" +
							   "ret stmt = " + parser.nRetStmt + "\n" +
							   "sysout stmt = " + parser.nSysOutStmt + "\n");
		consoleTextArea.append("Tree Weight = " + treeWeight + "\n");
		Iterator iter = parser.idNumMap.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String key = (String)entry.getKey();
			int val = (Integer)entry.getValue();
			consoleTextArea.append(key + ":" + val + "\n");
		}
	}
	
	/**
	 * This method initializes pronblemTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getProblemTextArea() {
		if (problemTextArea == null) {
			problemTextArea = new JTextArea();
			problemTextArea.setEditable(false);
		}
		return problemTextArea;
	}
	
	/**
	 * This method modifies problemTextArea
	 */
	private void setProblemTextArea(XYZ2 parser) {
		if(!lError) {
			problemTextArea.setText("");
		}
		String problems = parser.exceptions;
		if(problems != null && problems != "") {
			problemTextArea.append(problems);
		}
		else {
			if(!lError) {
				problemTextArea.append("No Exceptions");
			}
		}
		lError = false;
	}
	
	private void setProblemTextArea(String message) {
		problemTextArea.append(message);
	}
	
	/**
	 * This method initializes tokenTable
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTokenTable() {
		if (tokenTable == null) {
			tokenModel = new TableModel();
			tokenModel.setColumnCount(2);
			tokenModel.setColumnName(0, "TYPE");
			tokenModel.setColumnName(1, "TOKEN");
			tokenTable = new JTable(tokenModel);
			//consoleTable.setEditable(false);
		}
		return tokenTable;
	}

	/**
	 * This method modifies tokenTable
	 */
	private void setTokenTable(XYZ2 parser) {
		Token token = null;
		tokenModel.removeRows(0, tokenModel.getRowCount());
		while ((token = parser.getNextToken()).image != "") {
		    System.out.println(token.image);
	        tokenModel.addRow(XYZ2Constants.tokenImage[token.kind], token.image);
			//tokenTextArea.append(XYZ2Constants.tokenImage[token.kind] + " : " + token.image +"\n");
		    if(token.kind == XYZ2.WrongID) {
				problemTextArea.setText("");
		    	setProblemTextArea("Lexical Error: WrongID achieved.\n" +
		    					   "Perhaps there is number at the begining of ID.\n");
		    	lError = true;
		    }
		}
		tokenTable.updateUI();
	}
	
	/**
	 * This method initializes consoleTable
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getConsoleTable() {
		if (consoleTable == null) {
			consoleModel = new TableModel();
			consoleModel.setColumnCount(2);
			consoleModel.setColumnName(0, "KEY");
			consoleModel.setColumnName(1, "VALUE");
			consoleTable = new JTable(consoleModel);
			//consoleTable.setEditable(false);
		}
		return consoleTable;
	}

	/**
	 * This method modifies consoleTable
	 */
	private void setConsoleTable(XYZ2 parser) {
        parser.CountKeyWordAndId();
        consoleModel.removeRows(0, consoleModel.getRowCount());
        consoleModel.addRow("assign stmt", parser.nAssignStmt);
        consoleModel.addRow("sysout stmt", parser.nRetStmt);
        consoleModel.addRow("ret stmt", parser.nSysOutStmt);
		Iterator iter = parser.idNumMap.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String key = (String)entry.getKey();
			int val = (Integer)entry.getValue();
			consoleModel.addRow(key, val);
		}
        consoleModel.addRow("Tree Weight", treeWeight);
        consoleTable.updateUI();
	}
	
	
	
	
	
	
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel codePane = null;
	private JTree jTree = null;
	private JScrollPane jScrollPane = null;
	private JScrollPane scrCode = null;
	private JScrollPane tokenPane = null;
	private JScrollPane consolePane = null;
	private JScrollPane problemPane = null;
	private JScrollPane threeAddressCodePane = null;
	private JMenuBar _menuBar = null;
	private JMenu menuFile = null;
	private JMenu menuRun = null;
	private JMenu menuHelp = null;
	private JMenuItem menuItemOpen = null;
	private JMenuItem menuItemExit = null;
	private JMenuItem menuItemRun = null;
	private JMenuItem menuItemAbout = null;
	private JSplitPane splitPane = null;
	private JSplitPane mainSplit = null;
	private JSplitPane codeSplit = null;
	private JEditorPane codeEditor = null;
	private JToolBar toolBar = null;
	private JButton btnNew = null;
	private JButton btnOpen = null;
	private JButton btnParse = null;
	private JTabbedPane tabCodeEditor = null;
	private JTabbedPane tabOutputPane = null;
	private JTextArea tokenTextArea = null;
	private JTextArea consoleTextArea = null;
	private JTextArea problemTextArea = null;
	private JTextArea threeAddressCodeTextArea = null;
	private JTable consoleTable = null;
	private JTable tokenTable = null;
	private TableModel consoleModel = null;
	private TableModel tokenModel = null;
	
	private int treeWeight=0;
	private boolean lError = false;
	
	/**
	 * This is the default constructor
	 */
	public MainWindow() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(1280, 768);
		this.setVisible(true);
		this.setContentPane(getMainSplit());
		this.setJMenuBar(get_menuBar());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(1);
			jContentPane = new JPanel();
			jContentPane.setLayout(gridLayout);
			jContentPane.add(getSplitPane(), null);
			
			
		}
		return jContentPane;
	}
	
	/**
	 * This method initializes codePane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getCodePane() {
		if (codePane == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(1);
			codePane = new JPanel();
			codePane.setLayout(gridLayout);
			codePane.add(getCodeSplit(), null);
		}
		return codePane;
	}

	/**
	 * This method initializes jTree	
	 * 	
	 * @return javax.swing.JTree	
	 * @throws ParseException 
	 */
	private JTree getJTree() {
		if (jTree == null) {
			//jTree = new JTree();
			jTree = new JTree();
		}
		return jTree;
	}


	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setName("jScrollPane");
			jScrollPane.setViewportView(getJTree());
			
			//jScrollPane.setLayout(new BorderLayout());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes _menuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar get_menuBar() {
		if (_menuBar == null) {
			_menuBar = new JMenuBar();
			Color c = new Color(212,219,238,128);
			_menuBar.setBackground(c);
			_menuBar.add(getMenuFile());
			_menuBar.add(getMenuRun());
			_menuBar.add(getMenuHelp());
		}
		return _menuBar;
	}

	/**
	 * This method initializes menuFile	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getMenuFile() {
		if (menuFile == null) {
			menuFile = new JMenu();
			menuFile.setText("File");
			menuFile.add(getMenuItemOpen());
			menuFile.add(getMenuItemExit());
		}
		return menuFile;
	}
	
	/**
	 * This method initializes menuRun	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getMenuRun() {
		if (menuRun == null) {
			menuRun = new JMenu();
			menuRun.setText("Run");
			menuRun.add(getMenuItemRun());
			menuRun.add(getMenuItemSemanticAnalysis());
			menuRun.add(getMenuItemThreeAddressCode());
		}
		return menuRun;
	}
	
	/**
	 * This method initializes menuHelp	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getMenuHelp() {
		if (menuHelp == null) {
			menuHelp = new JMenu();
			menuHelp.setText("Help");
			menuHelp.add(getMenuItemAbout());
		}
		return menuHelp;
	}

	/**
	 * This method initializes menuItemOpen	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	@SuppressWarnings("serial")
	private JMenuItem getMenuItemOpen() {
		if (menuItemOpen == null) {
			menuItemOpen = new JMenuItem();
			menuItemOpen.setAction(new AbstractAction(){
				public void actionPerformed(ActionEvent e) {
					openFile();
				}});
			menuItemOpen.setText("Open");
		}
		return menuItemOpen;
	}
	
	/**
	 * This method initializes menuItemExit
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	@SuppressWarnings("serial")
	private JMenuItem getMenuItemExit() {
		if (menuItemExit == null) {
			menuItemExit = new JMenuItem();
			menuItemExit.setAction(new AbstractAction(){
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}});
			menuItemExit.setText("Exit");
		}
		return menuItemExit;
	}
	
	/**
	 * This method initializes menuItemRun	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	@SuppressWarnings("serial")
	private JMenuItem getMenuItemRun() {
		if (menuItemRun == null) {
			menuItemRun = new JMenuItem();
			menuItemRun.setAction(new AbstractAction(){
				public void actionPerformed(ActionEvent e) {
					parse();
				}});
			menuItemRun.setText("Run");
		}
		return menuItemRun;
	}
	/**
	 * This method initializes MenuItemSemanticAnalysis	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	@SuppressWarnings("serial")
	private JMenuItem getMenuItemSemanticAnalysis() {
		JMenuItem analysis = new JMenuItem();
		analysis.setAction(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				try {
					semanticAnalysis();
				} catch (_Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}}
		);
		analysis.setText("Semantic");
		
		return analysis;
	}
	/**
	 * This method initializes menuItemAbout
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	@SuppressWarnings("serial")
	private JMenuItem getMenuItemAbout() {
		if (menuItemAbout == null) {
			menuItemAbout = new JMenuItem();
			menuItemAbout.setAction(new AbstractAction(){
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null, 
							"SJTU",
							"About",
							JOptionPane.INFORMATION_MESSAGE);
				}});
			menuItemAbout.setText("About");
		}
		return menuItemAbout;
	}

	/**
	 * This method initializes toolBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private JToolBar getToolBar() {
		if (toolBar == null) {
			toolBar = new JToolBar();
			toolBar.add(getBtnOpen());
			toolBar.add(getBtnParse());
			toolBar.add(getBtnSemanticAnalysis());
			toolBar.add(getBtnThreeAddressCode());
		}
		return toolBar;
	}
	
	/**
	 * This method initializes btnOpen	
	 * 	
	 * @return javax.swing.JButton	
	 */
	@SuppressWarnings("serial")
	private JButton getBtnOpen() {
		if (btnOpen == null) {
			btnOpen = new JButton();
			//btnParse.setText("Parse");
			btnOpen.setAction(new AbstractAction("Open") {
				public void actionPerformed(ActionEvent e) {
					openFile();
				}
			});
		}
		return btnOpen;
	}
	
	/**
	 * This method initializes btnParse	
	 * 	
	 * @return javax.swing.JButton	
	 */
	@SuppressWarnings("serial")
	private JButton getBtnParse() {
		if (btnParse == null) {
			btnParse = new JButton();
			//btnParse.setText("Parse");
			btnParse.setAction(new AbstractAction("Parse") {
				public void actionPerformed(ActionEvent e) {
					parse();
				}
			});
		}
		return btnParse;
	}

	/**
	 * This method initializes BtnSemanticAnalysis	
	 * 	
	 * @return javax.swing.JButton	
	 */
	@SuppressWarnings("serial")
	private JButton getBtnSemanticAnalysis() {
		JButton btnAnalysis = new JButton();
		btnAnalysis.setAction(new AbstractAction("Semantic") {
			public void actionPerformed(ActionEvent e) {
				try {
					semanticAnalysis();
				} catch (_Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}}
		);
		
		return btnAnalysis;
	}
	
	protected void openFile() {
		JFileChooser fc = new JFileChooser();
		if (fc.showDialog(this, "Open")==JFileChooser.APPROVE_OPTION) {
			System.out.println(fc.getSelectedFile());
			File file = new File(fc.getSelectedFile().toString());
	        BufferedReader reader = null;
	        try {
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            String code = "";
	            while ((tempString = reader.readLine()) != null) {
	                code += (tempString + "\n");
	            }
	            codeEditor.setText(code);
	            reader.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
		}
		
	}

	/**
	 * This method initializes splitPane	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getSplitPane() {
		if (splitPane == null) {
			splitPane = new JSplitPane();
			splitPane.setDividerLocation(100);
			splitPane.setRightComponent(getJScrollPane());
			splitPane.setLeftComponent(getCodePane());
			splitPane.setDividerLocation(920);
			
		}
		return splitPane;
	}

	/**
	 * This method initializes codeEditor	
	 * 	
	 * @return javax.swing.JEditorPane	
	 */
	private JEditorPane getCodeEditor() {
		if (codeEditor == null) {
			DefaultSyntaxKit.initKit();
			codeEditor = new JEditorPane();
			//codeEditor.setContentType("text/java");
		}
		return codeEditor;
	}

	/**
	 * This method initializes scrCode	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getScrCode() {
		if (scrCode == null) {
			scrCode = new JScrollPane(getCodeEditor());
			scrCode.setBorder(null);
			scrCode.setViewportView(getCodeEditor());
			getCodeEditor().setContentType("text/java");
		}
		return scrCode;
	}
	
	/**
	 * This method initializes tokenPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getTokenPane() {
		if (tokenPane == null) {
			tokenPane = new JScrollPane(getTokenTable());
			tokenPane.setBorder(null);
			//tokenPane.setViewportView(getCodeEditor());
		}
		return tokenPane;
	}
	
	/**
	 * This method initializes consolePane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getConsolePane() {
		if (consolePane == null) {
			consolePane = new JScrollPane(getConsoleTable());
			consolePane.setBorder(null);
			//consolePane.setViewportView(getCodeEditor());
		}
		return consolePane;
	}
	
	/**
	 * This method initializes problemPane
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getProblemPane() {
		if (problemPane == null) {
			problemPane = new JScrollPane(getProblemTextArea());
			problemPane.setBorder(null);
			//problemPane.setViewportView(getCodeEditor());
		}
		return problemPane;
	}

	/**
	 * This method initializes mainSplit	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getMainSplit() {
		if (mainSplit == null) {
			mainSplit = new JSplitPane();
			mainSplit.setOrientation(JSplitPane.VERTICAL_SPLIT);
			mainSplit.setTopComponent(getToolBar());
			mainSplit.setBottomComponent(getJContentPane());
		}
		return mainSplit;
	}

	/**
	 * This method initializes codeSplit	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getCodeSplit() {
		if (codeSplit == null) {
			codeSplit = new JSplitPane();
			codeSplit.setOrientation(JSplitPane.VERTICAL_SPLIT);
			codeSplit.setTopComponent(getTabCodeEditor());
			codeSplit.setBottomComponent(getTabOutputPane());
			codeSplit.setDividerLocation(450);
		}
		return codeSplit;
	}
	
	/**
	 * This method initializes MenuItemThreeAddressCode
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	@SuppressWarnings("serial")
	private JMenuItem getMenuItemThreeAddressCode() {
		JMenuItem threeAddressCode = new JMenuItem();
		threeAddressCode.setAction(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				try {
					semanticAnalysis();
					setThreeAddressCodeTextArea();
				} catch (_Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}}
		);
		threeAddressCode.setText("3AC");
		
		return threeAddressCode;
	}
	/**
	 * This method initializes BtnThreeAddressCode	
	 * 	
	 * @return javax.swing.JButton	
	 */
	@SuppressWarnings("serial")
	private JButton getBtnThreeAddressCode() {
		JButton btnThreeAddressCode = new JButton();
		btnThreeAddressCode.setAction(new AbstractAction("3AC") {
			public void actionPerformed(ActionEvent e) {
				try {
					semanticAnalysis();
					setThreeAddressCodeTextArea();
				} catch (_Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}}
		);
		
		return btnThreeAddressCode;
	}
	/**
	 * This method initializes threeAddresCodePane
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getThreeAddressCodePane() {
		if (threeAddressCodePane == null) {
			threeAddressCodePane = new JScrollPane(getThreeAddressCodeTextArea());
			threeAddressCodePane.setBorder(null);
		}
		return threeAddressCodePane;
	}
	
	/**
	 * This method initializes threeAddressCodeTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getThreeAddressCodeTextArea() {
		if (threeAddressCodeTextArea == null) {
			threeAddressCodeTextArea = new JTextArea();
			threeAddressCodeTextArea.setEditable(false);
		}
		return threeAddressCodeTextArea;
	}
	
	private void setThreeAddressCodeTextArea() {
		threeAddressCodeTextArea.setText("");
		String tac = "";
		for(int i=0; i<TACGenerator.tacList.size(); i++) {
			tac += TACGenerator.tacList.get(i);
		}
		threeAddressCodeTextArea.append(tac);
	}
	
	private void ThreeAddressCode() {
		System.out.println("ThreeAddressCode");
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"

